package com.example.woke1;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.GlideApp;
import com.woke.fragment.Myzhong1Fragment;
import com.woke.fragment.Myzhong2Fragment;
import com.woke.fragment.Myzhong3Fragment;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import cn.xl.xxl.imagecache.ImageLoader;

public class MyzhongchouActivity extends FragmentActivity implements OnCheckedChangeListener {

    private FragmentTabHost mTabHost;
    private RadioGroup radioGroup;
    private ImageLoader imageLoader;
    private ImageView mIcon;
    private MyApp application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myzhongchou);

        intview();

    }

    @Override
    protected void onDestroy() {
        imageLoader.clearCache();
        imageLoader.clearMemoryCache();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        if (null != application.getDatas_load() && null != application.getDatas_load().getAvatar() && !application.getDatas_load()
                .getAvatar()
                .equals("")) {
            GlideApp.with(MyzhongchouActivity.this)
                    .load("http://" + application.getDatas_load().getAvatar())
                    .placeholder(R.mipmap.icn__launch_oem)
                    .error(R.mipmap.icn__launch_oem)
                    .into(mIcon);
//				imageLoader.DisplayImage("http://"+application.getDatas_load().getAvatar(), mIcon, false, true, true,0);
        }

    }

    private void intview() {
        imageLoader = new ImageLoader(this);
        application = (MyApp) getApplication();
        findViewById(R.id.myzhongchou_image_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.zhongc_text_ok).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyzhongchouActivity.this, MyzhongctypeActivity.class);
                startActivity(intent);
            }
        });
        TextView mTname = (TextView) findViewById(R.id.myzhongchou_text_phone);
        mIcon = (ImageView) findViewById(R.id.myzhongchou_icon);
        if (application != null && application.getDatas_load() != null) {
            mTname.setText(application.getDatas_load().getUser_login());
            imageLoader.DisplayImage("http://" + application.getDatas_load()
                    .getAvatar(), mIcon, false, true, true, 0);
        }
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
//		mLayout = (RelativeLayout) findViewById(R.id.avlayout_bottom);
        mTabHost.setHorizontalScrollBarEnabled(false);
        mTabHost.setVerticalScrollBarEnabled(false);
        mTabHost.setup(MyzhongchouActivity.this, getSupportFragmentManager(),
                R.id.avrealtabcontent);
        mTabHost.getTabWidget().setVisibility(View.GONE);

        mTabHost.addTab(mTabHost.newTabSpec("avindex").setIndicator("avHome"),
                Myzhong1Fragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("avinout").setIndicator("avOut"),
                Myzhong3Fragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("avmonitor").setIndicator("avMessage"),
                Myzhong2Fragment.class, null);
        mTabHost.setCurrentTabByTag("avindex");
        ((RadioButton) findViewById(R.id.avradio_index)).setChecked(true);
        radioGroup = (RadioGroup) findViewById(R.id.avradiogroup);
        radioGroup.setOnCheckedChangeListener(this);


    }

    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        FragmentManager fm = getSupportFragmentManager();
        Myzhong1Fragment avindex = (Myzhong1Fragment) fm.findFragmentByTag("avindex");
        Myzhong3Fragment avinout = (Myzhong3Fragment) fm.findFragmentByTag("avinout");
        Myzhong2Fragment avmonitor = (Myzhong2Fragment) fm.findFragmentByTag("avmonitor");
        FragmentTransaction ft = fm.beginTransaction();
        // ** Detaches the androidfragment if exists */
        if (avindex != null)
            ft.detach(avindex);
        if (avinout != null)
            ft.detach(avinout);
        if (avmonitor != null)
            ft.detach(avmonitor);
        switch (arg1) {
            case R.id.avradio_index:
                if (avindex == null) {
                    ft.add(R.id.avrealtabcontent, new Myzhong1Fragment(), "avindex");
                } else {
                    ft.attach(avindex);
                }
                mTabHost.setCurrentTabByTag("avindex");
                break;

            case R.id.avradio_out:
                if (avinout == null) {
                    ft.add(R.id.avrealtabcontent, new Myzhong3Fragment(), "avinout");
                } else {
                    ft.attach(avinout);
                }
                mTabHost.setCurrentTabByTag("avinout");
                break;

            case R.id.avradio_monotor:
                if (avmonitor == null) {
                    ft.add(R.id.avrealtabcontent, new Myzhong2Fragment(), "avmonitor");
                } else {
                    ft.attach(avmonitor);
                }
                mTabHost.setCurrentTabByTag("avmonitor");
                break;
        }
    }

}
